import React from 'react'
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom'
import { ConfigProvider, Layout } from 'antd'
import zhCN from 'antd/lib/locale/zh_CN'
import 'antd/dist/reset.css'
import './App.css'
import Header from './components/Layout/Header'
import Sidebar from './components/Layout/Sidebar'
import Dashboard from './pages/Dashboard'
import ContentCenter from './pages/ContentCenter'
import PublishManagement from './pages/PublishManagement'
import PushManagement from './pages/PushManagement'
import CommunityManagement from './pages/CommunityManagement'
import BrandPromotion from './pages/BrandPromotion'
import DataAnalysis from './pages/DataAnalysis'
import GovernmentService from './pages/GovernmentService'
import AgriculturalService from './pages/AgriculturalService'
import VillageService from './pages/VillageService'
import DataResourceCenter from './pages/DataResourceCenter'
import VillageManagement from './pages/VillageManagement'
import PublicOpinion from './pages/PublicOpinion'
import Login from './pages/Login'
import Register from './pages/Register'
import UserDashboard from './pages/UserDashboard'
import AdminDashboard from './pages/AdminDashboard'
import PolicyQuery from './pages/PolicyQuery'
import TrainingCenter from './pages/TrainingCenter'
import MarketAnalysis from './pages/MarketAnalysis'
import TechExchange from './pages/TechExchange'
import Events from './pages/Events'
import Profile from './pages/Profile'
import MediaIntegration from './pages/MediaIntegration'
import RuralService from './pages/RuralService'
import DigitalGovernance from './pages/DigitalGovernance'
import DigitalRuralConstruction from './pages/DigitalRuralConstruction'
import MediaIntegrationManagement from './pages/admin/MediaIntegrationManagement'
import RuralServiceManagement from './pages/admin/RuralServiceManagement'
import DigitalGovernanceManagement from './pages/admin/DigitalGovernanceManagement'
import DigitalRuralConstructionManagement from './pages/admin/DigitalRuralConstructionManagement'

const { Content } = Layout

// 权限路由组件
const ProtectedRoute = ({ children, isAdmin = false }: { children: React.ReactNode, isAdmin?: boolean }) => {
  const userInfo = localStorage.getItem('userInfo') || sessionStorage.getItem('userInfo')
  
  // 如果用户未登录，重定向到登录页面
  if (!userInfo) {
    return <Navigate to="/login" replace />
  }
  
  const parsedUserInfo = JSON.parse(userInfo)
  
  // 如果需要管理员权限但用户不是管理员，重定向到用户仪表盘
  if (isAdmin && !parsedUserInfo.isAdmin) {
    return <Navigate to="/user-dashboard" replace />
  }
  
  return children
}

const App: React.FC = () => {
  return (
    <ConfigProvider locale={zhCN}>
      <Router>
        <Routes>
          {/* 用户仪表盘页面 - 独立布局 */}
          <Route path="/user-dashboard" element={
            <ProtectedRoute>
              <UserDashboard />
            </ProtectedRoute>
          } />
          
          {/* 用户功能页面 - 独立布局 */}
          <Route path="/policy-query" element={
            <ProtectedRoute>
              <PolicyQuery />
            </ProtectedRoute>
          } />
          
          <Route path="/training-center" element={
            <ProtectedRoute>
              <TrainingCenter />
            </ProtectedRoute>
          } />
          
          <Route path="/market-analysis" element={
            <ProtectedRoute>
              <MarketAnalysis />
            </ProtectedRoute>
          } />
          
          <Route path="/tech-exchange" element={
            <ProtectedRoute>
              <TechExchange />
            </ProtectedRoute>
          } />
          
          <Route path="/events" element={
            <ProtectedRoute>
              <Events />
            </ProtectedRoute>
          } />
          
          <Route path="/profile" element={
            <ProtectedRoute>
              <Profile />
            </ProtectedRoute>
          } />
          
          <Route path="/media-integration" element={
            <ProtectedRoute>
              <MediaIntegration />
            </ProtectedRoute>
          } />
          
          <Route path="/rural-service" element={
            <ProtectedRoute>
              <RuralService />
            </ProtectedRoute>
          } />
          
          <Route path="/digital-governance" element={
            <ProtectedRoute>
              <DigitalGovernance />
            </ProtectedRoute>
          } />
          
          <Route path="/digital-rural-construction" element={
            <ProtectedRoute>
              <DigitalRuralConstruction />
            </ProtectedRoute>
          } />
          
          {/* 登录和注册页面 - 无布局 */}
          <Route path="/login" element={<Login />} />
          <Route path="/register" element={<Register />} />
          
          {/* 管理后台页面 - 统一布局 */}
          <Route path="*" element={
            <Layout style={{ minHeight: '100vh' }}>
              <Header />
              <Layout>
                <Sidebar />
                <Content style={{ margin: '0 16px 0 256px', minHeight: 'calc(100vh - 64px)' }}>
                  <div style={{ minHeight: 360 }}>
                    <Routes>
                      {/* 受保护的管理员路由 */}
                      <Route path="/admin-dashboard" element={
                        <ProtectedRoute isAdmin={true}>
                          <AdminDashboard />
                        </ProtectedRoute>
                      } />
                      
                      {/* 原有管理后台页面 - 需要管理员权限 */}
                      <Route path="/" element={
                        <ProtectedRoute isAdmin={true}>
                          <Dashboard />
                        </ProtectedRoute>
                      } />
                      <Route path="/dashboard" element={
                        <ProtectedRoute isAdmin={true}>
                          <Dashboard />
                        </ProtectedRoute>
                      } />
                      <Route path="/content-center" element={
                        <ProtectedRoute isAdmin={true}>
                          <ContentCenter />
                        </ProtectedRoute>
                      } />
                      <Route path="/publish-management" element={
                        <ProtectedRoute isAdmin={true}>
                          <PublishManagement />
                        </ProtectedRoute>
                      } />
                      <Route path="/push-management" element={
                        <ProtectedRoute isAdmin={true}>
                          <PushManagement />
                        </ProtectedRoute>
                      } />
                      <Route path="/community-management" element={
                        <ProtectedRoute isAdmin={true}>
                          <CommunityManagement />
                        </ProtectedRoute>
                      } />
                      <Route path="/brand-promotion" element={
                        <ProtectedRoute isAdmin={true}>
                          <BrandPromotion />
                        </ProtectedRoute>
                      } />
                      <Route path="/data-analysis" element={
                        <ProtectedRoute isAdmin={true}>
                          <DataAnalysis />
                        </ProtectedRoute>
                      } />
                      <Route path="/government-service" element={
                        <ProtectedRoute isAdmin={true}>
                          <GovernmentService />
                        </ProtectedRoute>
                      } />
                      <Route path="/agricultural-service" element={
                        <ProtectedRoute isAdmin={true}>
                          <AgriculturalService />
                        </ProtectedRoute>
                      } />
                      <Route path="/village-service" element={
                        <ProtectedRoute isAdmin={true}>
                          <VillageService />
                        </ProtectedRoute>
                      } />
                      <Route path="/data-resource-center" element={
                        <ProtectedRoute isAdmin={true}>
                          <DataResourceCenter />
                        </ProtectedRoute>
                      } />
                      <Route path="/village-management" element={
                        <ProtectedRoute isAdmin={true}>
                          <VillageManagement />
                        </ProtectedRoute>
                      } />
                      <Route path="/public-opinion" element={
                        <ProtectedRoute isAdmin={true}>
                          <PublicOpinion />
                        </ProtectedRoute>
                      } />
                      
                      {/* 新增管理后台模块路由 */}
                      <Route path="/admin/media-integration" element={
                        <ProtectedRoute isAdmin={true}>
                          <MediaIntegrationManagement />
                        </ProtectedRoute>
                      } />
                      <Route path="/admin/rural-service" element={
                        <ProtectedRoute isAdmin={true}>
                          <RuralServiceManagement />
                        </ProtectedRoute>
                      } />
                      <Route path="/admin/digital-governance" element={
                        <ProtectedRoute isAdmin={true}>
                          <DigitalGovernanceManagement />
                        </ProtectedRoute>
                      } />
                      <Route path="/admin/digital-rural-construction" element={
                        <ProtectedRoute isAdmin={true}>
                          <DigitalRuralConstructionManagement />
                        </ProtectedRoute>
                      } />
                    </Routes>
                  </div>
                </Content>
              </Layout>
            </Layout>
          } />
        </Routes>
      </Router>
    </ConfigProvider>
  )
}

export default App